package edebe.flyinginstrument.event;

import edebe.flyinginstrument.entity.IFlyingInstrumentEntity;
import edebe.flyinginstrument.item.IFlyingInstrumentItem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@Mod.EventBusSubscriber
public class EventHander {

    @SubscribeEvent
    @SideOnly(Side.CLIENT)
    public static void onGameOverlayRender(RenderGameOverlayEvent.Pre event) {
        EntityPlayerSP player = Minecraft.getMinecraft().player;
        if (event.getType() == RenderGameOverlayEvent.ElementType.HEALTHMOUNT && player.isRiding() && player.getRidingEntity() instanceof IFlyingInstrumentEntity) event.setCanceled(true);
    }

    @SubscribeEvent
    public static void onPlayerRightClickItem(PlayerInteractEvent.RightClickItem event) {
        World world = event.getWorld();
        if (!world.isRemote) {
            EntityPlayer player = event.getEntityPlayer();
            if (player != null) {
                ItemStack stack = event.getItemStack();
                NBTTagCompound data = player.getEntityData();
                if (data.hasKey("Power") && data.getFloat("Power") > 0 && (!player.isRiding() && !player.isRiding() && stack.getItem() instanceof IFlyingInstrumentItem)) {
                    stack.damageItem(1, player);
                    if (!player.isSneaking() && stack.getItem() instanceof IFlyingInstrumentItem) {
                        Entity sword = ((IFlyingInstrumentItem) stack.getItem()).createEntity(world, stack.copy(), player);
                        sword.setPositionAndUpdate(player.posX, player.posY, player.posZ);
                        world.spawnEntity(sword);
                        event.getEntity().startRiding(sword);
                        stack.shrink(1);
                    }
                }else if (stack.getItem() instanceof IFlyingInstrumentItem && data.hasKey("Power") && data.getFloat("Power") <= 0){
                    player.sendMessage(new TextComponentTranslation("message.flyinginstrument.use.flysword.fail"));
                }
            }
        }

//
//        if (!world.isRemote) {
//
//            EntityPlayer player = event.getEntityPlayer();
//            if (player != null) {
//                ItemStack stack = event.getItemStack();
//                if (stack.getItem() instanceof IFlyingInstrumentItem) {
//                    Entity sword = ((IFlyingInstrumentItem) stack.getItem()).createEntity(world, stack.copy(), player);
//                    sword.setPositionAndUpdate(player.posX, player.posY, player.posZ);
//                    world.spawnEntity(sword);
//                    event.getEntity().startRiding(sword);
//                    stack.shrink(1);
//
//                }
//            }
//        }



    }

    @SubscribeEvent
    public static void onPlayerUpdate(TickEvent.PlayerTickEvent event) {
        EntityPlayer player = event.player;
        if (player.isRiding() && player.isSneaking()) {
            Entity riding = player.getRidingEntity();
            if (riding instanceof IFlyingInstrumentEntity) {
                ((IFlyingInstrumentEntity) riding).putAwaySword(player);
                player.dismountRidingEntity();
            }
        }
    }



    @SubscribeEvent
    public static void onPlayerLoggedOutEvent(PlayerEvent.PlayerLoggedOutEvent event){
        if(event != null) {
            EntityPlayer player = event.player;
            if (player != null && player instanceof EntityPlayer && !player.world.isRemote) {

                if (player.isRiding()) {
                    Entity riding = player.getRidingEntity();
                    if (riding!=null && riding instanceof IFlyingInstrumentEntity) {
                        ((IFlyingInstrumentEntity) riding).putAwaySword(player);
                        player.dismountRidingEntity();
                    }
                }
            }
        }
    }


    @SubscribeEvent
    public static void onPlayerAttackEntityEvent(AttackEntityEvent event){
        if(event != null) {
            EntityPlayer player = event.getEntityPlayer();
            if (player != null &&  !player.world.isRemote) {
                ItemStack itemStack = player.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND);
                if(!itemStack.isEmpty()&&itemStack.getItem() instanceof IFlyingInstrumentItem){
                    event.setCanceled(true);
                }
            }
        }
    }

}
